package com.yvon.grpc.client.controller;

import com.google.common.collect.Lists;
import com.yvon.grpc.api.Ip;
import com.yvon.grpc.client.service.CalculateClientService;
import com.yvon.grpc.client.service.HelloWorldClientService;
import com.yvon.grpc.client.service.NameServersClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : wangyufeng
 * @since : 2021-10-19
 */
@RestController
public class HelloWorldController {

    @Autowired
    private HelloWorldClientService helloWorldClientService;

    @Autowired
    private NameServersClientService nameServersClientService;

    @Autowired
    private CalculateClientService calculateClientService;

    @GetMapping("/hello/{message}")
    public String helloWorld(@PathVariable String message) {
        return helloWorldClientService.rpc(message);
    }

    @GetMapping("/name/{message}")
    public String name(@PathVariable String message) {
        List<Ip> ipsByName = nameServersClientService.getIpsByName(message);
        List<String> collect = ipsByName.stream().map(Ip::getIp).collect(Collectors.toList());
        return collect.toString();
    }

    @GetMapping("calcul")
    public void calcul() {
        calculateClientService.getResult(Lists.newArrayList(1,3,6,9,2,97,33, 29, 43,455));
    }
}
